package com.example.farmeri.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.ProgressBar;

import androidx.constraintlayout.widget.Group;

import com.example.farmeri.R;
import com.example.farmeri.pojo.User;
import com.example.farmeri.presenter.login.IRegisterContract;
import com.example.farmeri.presenter.login.RegisterPresenter;
import com.example.farmeri.tools.ResponseUtils;
import com.example.farmeri.ui.base.BaseActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册
 */
public class SignUpActivity extends BaseActivity<RegisterPresenter> implements IRegisterContract {

    //昵称
    @BindView(R.id.sign_up_login_name)
    EditText edit_loginName;
    //电话
    @BindView(R.id.sign_up_tel)
    EditText edit_tel;
    //邮箱
    @BindView(R.id.sign_up_email)
    EditText edit_email;
//    //访问身份
//    @BindView(R.id.sign_up_identity)
//    EditText edit_identity;
    // 常用地址
    @BindView(R.id.sign_up_address)
    EditText edit_address;
    // 年龄
    @BindView(R.id.sign_up_age)
    EditText edit_age;
    //加载框
    @BindView(R.id.sign_up_progress)
    ProgressBar progressBar;

    @BindView(R.id.sign_up_group)
    Group viewGroup;

    //输入框集合
    @BindViews({R.id.sign_up_login_name,
            R.id.sign_up_tel,
            R.id.sign_up_email,
//            R.id.sign_up_identity,
            R.id.sign_up_address,
            R.id.sign_up_age
    })
    List<EditText> formList;

    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        ButterKnife.bind(this);
        present = new RegisterPresenter(this);
    }


    /**
     * 注册提交
     */
    @OnClick(R.id.sign_up_button)
    void register(){
        //有未输入的内容直接跳过
        if(!present.checkData(this,formList)){
            return;
        }
        //填充注册表单的内容
        setForm();
        present.register(user);
    }

    /**成都大学 湿度
     * 设置表单内容
     */
    private void setForm() {
        user = new User();
        user.setAge(edit_age.getText().toString());
        user.setEmail(edit_email.getText().toString());
        user.setIdentity("");
        user.setRequest_place(edit_address.getText().toString());
        user.setLogin_name(edit_loginName.getText().toString());
        user.setTel(edit_tel.getText().toString());
        Log.d("pojo", "setForm: User = "+user.toString());
    }

    @Override
    public void onRegisterSuccess(ResponseUtils<User> responseUtils) {
        if (responseUtils.getCode() == 200){
            Log.d("response", "onMessageEvent: response = " + responseUtils.toString());
            showToast("注册成功");
            startActivity(new Intent(this, SignInActivity.class));
        }else{
            showToast(this,responseUtils.getMessage());
        }
    }

    @Override
    public void showMessage(String msg) {
        showToast(this,msg);
    }
}

